package com.anos.smsfun.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.anos.smsfun.R;
import com.anos.smsfun.model.ContactsWrapper;
import com.anos.smsfun.utility.StringMatcher;

public class ContactsAdapter extends BaseAdapter implements SectionIndexer {

	private Context mContext;
	private ArrayList<ContactsWrapper> mData;
	private static LayoutInflater inflater = null;

	private String mSections = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	public ContactsAdapter(Context c, ArrayList<ContactsWrapper> data) {
		mContext = c;
		mData = data;
		inflater = LayoutInflater.from(mContext);

		// sort list data
		Collections.sort(mData, new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				ContactsWrapper p1 = (ContactsWrapper) o1;
				ContactsWrapper p2 = (ContactsWrapper) o2;
				return p1.getDisplayName().compareToIgnoreCase(
						p2.getDisplayName());
			}
		});
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		return 0;
	}

	/** Create a holder Class to contain inflated xml file elements */
	public static class ViewHolder {
		public ImageView imv;
		public TextView lblName;
		public TextView lblNumber;
		public CheckBox chkBox;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder = null;
		if (vi == null) {
			vi = inflater.inflate(R.layout.item_contact_list, null);

			holder = new ViewHolder();
			holder.imv = (ImageView) vi
					.findViewById(R.id.imvPic_item_contact_list);
			holder.lblName = (TextView) vi
					.findViewById(R.id.lblName_item_contac_list);
			holder.lblNumber = (TextView) vi
					.findViewById(R.id.lblNumber_item_contact_list);
			holder.chkBox = (CheckBox) vi
					.findViewById(R.id.chkSelect_item_contact_list);
			holder.chkBox.setVisibility(View.GONE);

			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		if (this.mData.size() > position) {
			holder.lblName.setText(this.mData.get(position).getDisplayName());
			holder.lblNumber.setText(this.mData.get(position)
					.getDisplayNumber());
		}

		return vi;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	@Override
	public int getSectionForPosition(int position) {
		return 0;
	}

	@Override
	public int getPositionForSection(int sectionIndex) {
		for (int i = sectionIndex; i >= 0; i--) {
			for (int j = 0; j < getCount(); j++) {
				if (i == 0) {
					// For numeric section
					for (int k = 0; k <= 9; k++) {
						if (StringMatcher.match(
								String.valueOf(mData.get(j).getDisplayName()
										.charAt(0)), String.valueOf(k)))
							return j;
					}
				} else {
					if (StringMatcher.match(
							String.valueOf(mData.get(j).getDisplayName()
									.charAt(0)),
							String.valueOf(mSections.charAt(i))))
						return j;
				}
			}
		}
		return 0;
	}

	@Override
	public Object[] getSections() {
		String[] sections = new String[mSections.length()];
		for (int i = 0; i < mSections.length(); i++) {
			sections[i] = String.valueOf(mSections.charAt(i));
		}
		return sections;
	}

}
